package com.dingding.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.study.Event.serviceImpl.DomainEventPublisher;
import com.dingding.study.annotation.ListParam;
import com.dingding.study.entity.User;
import com.dingding.study.mapper.UserMapper;
import com.dingding.study.service.UserService;
import com.dingding.study.service.事件发布机制.Event;
import com.dingding.study.service.事件发布机制.EventListener;
import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author liudingding
 * @since 2020-06-26 00:10:38
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Resource
    private UserMapper userDao;

    @Autowired
    UserServiceBImpl userService2;

    public void addUser(@ListParam List<User> userList) {
        System.out.println("添加用户开始...");
        for (User user : userList) {
            System.out.println("用户名：" + user.getUsername() + "，密码：" + user.getPassword());
        }
        System.out.println("添加用户结束...");
    }


    @Transactional
    public void insert(){
        User user = new User();
        user.setUsername("liugongding");
        this.save(user);
    }

    public void updateUser() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", 32);
        User user = new User();
        user.setUsername("password");
        this.update(user, queryWrapper);
    }

    @Transactional
    public void testTransactional() {
        insert();
        updateUser();
        int a = 1/0;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public User queryById(Integer id) {

        boolean liu1 = this.save(
                User.builder()
                        .username("liu1")
                        .build()
        );
//        MyEventBus.post(new Message(Message.MessageType.OPENDOOR,"芝麻开门！"));
//

//        EventBus eventBus = new EventBus("test");
//        //注册监听到eventBus
//        eventBus.register(new EventListener());
//        //通过eventBus发送两笔消息
//        eventBus.post(new Event("send message1", new User()));

        UserCreatedEvent event = new UserCreatedEvent(new User());
        DomainEventPublisher.getInstance().publish(event);
        log.info("当前线程：{}", Thread.currentThread().getName());
        return null;
    }

    @Override
    @Transactional
    public void insert(User user) {

        boolean save = this.save(user);

        EventBus eventBus = new EventBus();
        //注册监听到eventBus
        eventBus.register(new EventListener());
        //通过eventBus发送两笔消息
        eventBus.post(new Event("插入数据", save));

//        int a = 1/0;

        System.out.println("当前线程："+Thread.currentThread().getName());
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
//    @Override
//    public User insert(User user) {
//        this.userDao.insert(user);
//        return user;
//    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public User queryUserByName(String username) {
        return userDao.queryUserByName(username);
    }

    public List<User> sort(){
        //List<User> list = new ArrayList<>();
        //User user1 = new User();
        //user1.setPass(false);
        //list.add(user1);
        //User user2 = new User();
        //user2.setPass(true);
        //list.add(user2);
        //Comparator<User> comparator = (o1, o2) -> {
        //    if (o1.isPass() ^ o2.isPass()) {
        //        return o1.isPass() ? -1 : 1;
        //    } else {
        //        return 0;
        //    }
        //};
        //list.sort(comparator);
        //return list;
        return null;
    }
}
